<?php

namespace app\console\controller;

use app\service\controller\File;
use app\console\controller\Auth;

/**
 * 站点设置controller
 * */
class System extends Auth
{
    public function __construct()
    {
        parent::__construct();
        $this->assign('active', 'system');

    }

    public function index()
    {
        $this->redirect('domain_config');
    }

    /**
     * 站点设置
     * */
    public function domain_config()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            $logo_res = (new File())->upload_single_img('domain_logo', 'logo', 95);
            if (is_array($logo_res)) {
                if ($logo_res['code'] == 1) {
                    $data['domain_logo'] = $logo_res['img_path'];
                } else {
                    $this->error($logo_res['msg']);
                }
            } else {
                unset($data['domain_logo']);
            }

            $defalut_pic_res = (new File())->upload_single_img('domain_default_pic', 'logo', 95);
            if (is_array($defalut_pic_res)) {
                if ($defalut_pic_res['code'] == 1) {
                    $data['domain_default_pic'] = $defalut_pic_res['img_path'];
                } else {
                    $this->error($defalut_pic_res['msg']);
                }
            } else {
                unset($data['domain_default_pic']);
            }


            $wechat_img_res = (new File())->upload_single_img('domain_wechat_img', 'logo', 90);
            if (is_array($wechat_img_res)) {
                if ($wechat_img_res['code'] == 1) {
                    $data['domain_wechat_img'] = $wechat_img_res['img_path'];
                } else {
                    $this->error($wechat_img_res['msg']);
                }
            } else {
                unset($data['domain_wechat_img']);
            }

            $old_config = APP_PATH . "console" . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . "domain_config.php";
            $merge = array_merge(include $old_config, $data);
            $new_config = "<?php \n\r return " . var_export($merge, true) . ";?>";
            $result = file_put_contents($old_config, $new_config);
            $this->success('配置成功');
            exit;
        }

        $this->assign([
            'topbar' => ['系统设置', '站点设置'],
        ]);
        return $this->fetch();
    }

    /**
     * 参数设置
     * */
    public function parm_config()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            // 可上传的图片后缀
            if (isset($data['img_suffix'])) {
                $data['img_suffix'] = implode(',', $data['img_suffix']);
            }

            // 水印图 路径
            $water_res = (new File())->upload_single_img('img_water', 'water');
            if (is_array($water_res)) {
                if ($water_res['code'] == 1) {
                    $data['img_water'] = $water_res['img_path'];
                } else {
                    $this->error($water_res['msg']);
                }
            } else {
                unset($data['img_water']);
            }

            $old_config = APP_PATH . "console" . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . "parm_config.php";
            $merge = array_merge(include $old_config, $data);
            $new_config = "<?php \n\r return " . var_export($merge, true) . ";?>";
            $result = file_put_contents($old_config, $new_config);
            $this->success('配置成功');
            exit;
        }
        $this->assign([
            'topbar' => ['系统设置', '参数设置'],
        ]);
        return $this->fetch();
    }

}
